package com.cdtu.labpreview.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdtu.labpreview.common.Result;
import com.cdtu.labpreview.controller.vo.InstrumentVo;
import com.cdtu.labpreview.controller.vo.ReferenceVo;
import com.cdtu.labpreview.entity.Class;
import com.cdtu.labpreview.entity.InstrumentIntroduction;
import com.cdtu.labpreview.entity.Reference;
import com.cdtu.labpreview.entity.Student;
import com.cdtu.labpreview.mapper.ClassMapper;
import com.cdtu.labpreview.mapper.ReferenceMapper;
import com.cdtu.labpreview.mapper.StudentMapper;
import com.cdtu.labpreview.service.IReferenceService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class ReferenceServiceImpl extends ServiceImpl<ReferenceMapper, Reference> implements IReferenceService {

    @Autowired
    private ReferenceMapper referenceMapper;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private ClassMapper classMapper;
    @Override
    public Result getReference(int no, int type) {
        if(type==1){//学生
            //根据学号获取班级id
            LambdaQueryWrapper<Student> lqw=new LambdaQueryWrapper<>();
            lqw.eq(Student::getSno,no);
            Student student = studentMapper.selectOne(lqw);
            long classId = student.getClassId();
            //根据班级id获取对应老师发布的仪器介绍
            LambdaQueryWrapper<Reference> lqw2=new LambdaQueryWrapper<>();
            lqw2.eq(Reference::getClassId,classId);
            List<Reference> references = referenceMapper.selectList(lqw2);
            List<ReferenceVo> collect = references.stream().map(instrumentIntroduction -> {

                String ref = instrumentIntroduction.getReferenceData();
                String[] referenceList=null;
                if(ref!=null){
                    referenceList  = ref.split(",");
                }
                ReferenceVo iv = new ReferenceVo();
                BeanUtils.copyProperties(instrumentIntroduction, iv);
                iv.setReferenceList(referenceList);
                return iv;
            }).collect(Collectors.toList());
            return Result.ok(collect);
        }else {//老师
            LambdaQueryWrapper<Reference> lqw=new LambdaQueryWrapper<>();
            lqw.eq(Reference::getTno,no);
            List<Reference> instrumentIntroductions = referenceMapper.selectList(lqw);
            List<ReferenceVo> collect = instrumentIntroductions.stream().map(instrumentIntroduction -> {
                long classId = instrumentIntroduction.getClassId();
                Class aClass = classMapper.selectById(classId);
                String cname = aClass.getName();

                String ref = instrumentIntroduction.getReferenceData();
                String[] referenceList=null;
                if(ref!=null){
                    referenceList  = ref.split(",");
                }
                ReferenceVo iv = new ReferenceVo();
                BeanUtils.copyProperties(instrumentIntroduction, iv);
                iv.setClassName(cname);
                iv.setReferenceList(referenceList);
                return iv;
            }).collect(Collectors.toList());
            return Result.ok(collect);
        }
    }

    @Override
    public Result addReference(Reference reference) {
        int flag = referenceMapper.insert(reference);
        if(flag<=0) return Result.fail("添加失败！请联系管理员");
        return Result.ok();
    }

    @Override
    public Result updateReference(Reference reference) {
        int flag = referenceMapper.updateById(reference);
        if(flag<=0) return Result.fail("修改失败！请联系管理员");
        return Result.ok();
    }

    @Override
    public Result deleteReference(long id) {
        int flag = referenceMapper.deleteById(id);
        if(flag<=0) return Result.fail("删除失败！请联系管理员");
        return Result.ok();
    }
}
